﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Vmago.Models
{
    public class RepositoryCity : ICity
    {
 
        private CityDataContext dataContext;

        public RepositoryCity()
        {
            dataContext = new CityDataContext();
        }

        public IEnumerable<Country> getCountry()
        {
            IEnumerable<Country> c = null;

            try
            {

                c = from p in dataContext.Countries
                    select p;
            }
            catch (Exception ex)
            {
                dataContext.Dispose();
                Console.WriteLine(ex.ToString());
                throw;
            }

            return c;
        }

        public IEnumerable<Country> getCountry(int saveCityID)
        {
            IEnumerable<Country> c = null;
            try
            {
                c = from p in dataContext.Countries
                    where p.countryID.Equals(saveCityID)
                    select p;
            }
            catch (Exception ex)
            {
                dataContext.Dispose();
                Console.WriteLine(ex.ToString());
                throw;
            }

            return c;
        }

        public IEnumerable<Province> getProvince(int CountryID)
        {
            IEnumerable<Province> c = null;
            try
            {
                c = from p in dataContext.Provinces
                    where p.countryID.Equals(CountryID)
                    select p;
            }
            catch (Exception ex)
            {
                dataContext.Dispose();
                Console.WriteLine(ex.ToString());
                throw;
            }
            return c;
        }

        public IEnumerable<City> getCity(int CountryID, int ProvinceID)
        {
            IEnumerable<City> c = null;
            try
            {
                c = from p in dataContext.Cities
                    where p.provinceID.Equals(ProvinceID)
                    select p;
            }
            catch (Exception ex)
            {
                dataContext.Dispose();
                Console.WriteLine(ex.ToString());
                throw;
            }
            return c;
        }

        public IEnumerable<City> getCity(int CityID)
        {
            IEnumerable<City> c = null;
            try
            {
                c = from p in dataContext.Cities
                    where p.cityID.Equals(CityID)
                    select p;
            }
            catch (Exception ex)
            {
                dataContext.Dispose();
                Console.WriteLine(ex.ToString());
                throw;
            }
            return c;
        }

        public IEnumerable<Suburb> getSuburb(int ProvinceID)
        {
            IEnumerable<Suburb> c = null;
            try
            {
                c = from p in dataContext.Suburbs
                    where p.provinceID.Equals(ProvinceID)
                    select p;
            }
            catch (Exception ex)
            {
                dataContext.Dispose();
                Console.WriteLine(ex.ToString());
                throw;
            }
            return c;
        }

        public IEnumerable<Suburb> getSuburb(int ProvinceID, int CityID)
        {
            IEnumerable<Suburb> c = null;

            try
            {
                c = from p in dataContext.Suburbs
                    where p.provinceID.Equals(ProvinceID) && p.cityID.Equals(CityID)
                    select p;
            }
            catch (Exception ex)
            {
                dataContext.Dispose();
                Console.WriteLine(ex.ToString());
                throw;
            }
            return c;
        }

        //This function is used to get the recommendation city and its description;
        //This function is used to get the recommendation city and its description based on CityId
        //If the grade is 1, it will be the highest priority and shown in the home page
        public IEnumerable<RecArea> getRecArea(int Grade, int CityID)
        {

            IEnumerable<RecArea> c = null;
            try
            {

                if (CityID == 0)
                {
                    c = from p in dataContext.RecAreas
                        where p.grade.Equals(Grade)
                        select p;

                }
                if (Grade == 0)
                {
                    c = from p in dataContext.RecAreas
                        where p.ID.Equals(CityID)
                        select p;
                }
            }
            catch (Exception ex)
            {
                dataContext.Dispose();
                Console.WriteLine(ex.ToString());
                throw;
            }
            return c;
        }


        public List<RecArea> getRecCity(int Grade, int perPage, int page)
        {
            List<RecArea> c = null;
            try
            {
                c = dataContext.getRecCity(Grade, perPage, page).ToList();
            }
            catch (Exception ex)
            {
                dataContext.Dispose();
                Console.WriteLine(ex.ToString());
                throw;
            }
            return c;
        }

        public IEnumerable<CityInfo> getCityInfo(int CityID)
        {
            IEnumerable<CityInfo> c = null;
            try
            {

                c = from p in dataContext.CityInfos
                    where p.cityID.Equals(CityID)
                    select p;
            }
            catch (Exception ex)
            {
                dataContext.Dispose();
                Console.WriteLine(ex.ToString());
                throw;
            }
            return c;
        }

        
    }
}
